package com.video.web.controller.video;

import com.video.common.core.domain.entity.SysUser;
import com.video.common.exception.ServiceException;
import com.video.common.utils.StringUtils;
import com.video.common.utils.uuid.IdUtils;
import com.video.system.service.ISysUserService;
import com.video.video.domain.DGoodsBach;
import com.video.video.domain.vo.GoodsQuery;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.video.common.annotation.Log;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.enums.BusinessType;
import com.video.video.domain.DGoods;
import com.video.video.service.IDGoodsService;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/video/DGoods")
public class DGoodsController extends BaseController
{
    @Autowired
    private IDGoodsService dGoodsService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsQuery dGoods)
    {
        startPage();
        List<DGoods> list = dGoodsService.selectDGoodsList(dGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsQuery dGoods)
    {
        List<DGoods> list = dGoodsService.selectDGoodsList(dGoods);
        ExcelUtil<DGoods> util = new ExcelUtil<DGoods>(DGoods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dGoodsService.selectDGoodsById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DGoods dGoods) {
        int j=0;
        for (int i = 1; i <= dGoods.getVideoNum(); i++) {
            dGoods.setId(System.currentTimeMillis()+""+i);
            dGoods.setVideoUrl(dGoods.getVideoPrefix()+"/"+i+"."+ dGoods.getVideoFormat());
            dGoods.setCreateBy(String.valueOf(getLoginUser().getUserId()));
            dGoods.setDownloadFlg("1");//下载标记 1 未下载 2 已下载
            j+=dGoodsService.insertDGoods(dGoods);
        }
        /*if (StringUtils.isNotEmpty(dGoods.getVideoUrl())){
            String[] ary = dGoods.getVideoUrl().split(",");
            for (String url : ary) {
                if (StringUtils.isNotEmpty(url)){
                    dGoods.setVideoUrl(url);
                    dGoods.setCreateBy(String.valueOf(getLoginUser().getUserId()));
                    dGoods.setDownloadFlg("1");//下载标记 1 未下载 2 已下载
                    j+=dGoodsService.insertDGoods(dGoods);
                }
            }
        }*/
        return toAjax(j);
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DGoods dGoods)
    {
        return toAjax(dGoodsService.updateDGoods(dGoods));
    }

    /**
     * 批量修改商品
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:editBach')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/editBach")
    public AjaxResult editBach(@RequestBody DGoodsBach dGoodsBach)
    {
        return toAjax(dGoodsService.updateDGoodsBach(dGoodsBach));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('video:DGoods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dGoodsService.deleteDGoodsByIds(ids));
    }

}
